package com.sojson.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.sojson.constant.Constant;
import com.sojson.enums.core.EGeneralStatus;
import com.sojson.project.sys.menu.entity.vo.MenuVo;
import com.sojson.project.sys.role.entity.vo.RoleVo;
import com.sojson.project.sys.role.service.RoleService;
import com.sojson.util.token.TokenUtil;

/**
 * 角色工具类
 *
 * @author lyh
 */
public class RoleUtil {

    private static RoleService roleService = SpringUtil.getBeanByType(RoleService.class);

    /**
     * 发送刷新权限的消息
     */
    public static void refreshPerms() {
        run();
    }

    /**
     * 刷新权限
     */
    public static void run() {
        Map<String, String> ROLE_NAMES = TokenUtil.ROLE_NAMES;
        Map<String, String> ROLE_KEYS = TokenUtil.ROLE_KEYS;
        Map<String, Set<String>> PERM_IDS = TokenUtil.PERM_IDS;
        Map<String, Set<String>> PERM_IDS_EXIST = TokenUtil.PERM_IDS_EXIST;
        Map<String, Set<String>> PERMS = TokenUtil.PERMS;
        try {
            Map<String, String> roleNames = new HashMap<>();
            Map<String, String> roleKeys = new HashMap<>();
            Map<String, Set<String>> permIds = new HashMap<>();
            Map<String, Set<String>> permIdsExist = new HashMap<>();
            Map<String, Set<String>> perms = new HashMap<>();

            List<RoleVo> roles = roleService.getPerms();
            for (RoleVo role : roles) {
                String id = role.getId();
                Set<MenuVo> pExist = role.getPerms();
                Set<MenuVo> p = new HashSet<>();
                removeDisableMenu(pExist, Constant.MENU_PARENT_ID, p);

                // 设置角色名称
                roleNames.put(id, role.getName());
                // 设置角色Key
                roleKeys.put(id, role.getKey());

                // 设置权限ID组
                permIds.put(id, p.stream().map(MenuVo::getId).collect(Collectors.toSet()));
                permIdsExist.put(id, pExist.stream().map(MenuVo::getId).collect(Collectors.toSet()));
                // 设置权限组
                perms.put(id, p.stream().map(MenuVo::getPerms).collect(Collectors.toSet()));
            }

            // 替换权限组
            TokenUtil.ROLE_NAMES = roleNames;
            TokenUtil.ROLE_KEYS = roleKeys;
            TokenUtil.PERM_IDS = permIds;
            TokenUtil.PERM_IDS_EXIST = permIdsExist;
            TokenUtil.PERMS = perms;
        } catch (Exception e) {
            ROLE_NAMES.clear();
            ROLE_KEYS.clear();
            PERM_IDS.clear();
            PERM_IDS_EXIST.clear();
            PERMS.clear();
        }
    }

    /**
     * 删除禁用掉的菜单
     * 
     * @param menus
     * @param parentId
     * @param p
     * @return
     */
    private static Set<MenuVo> removeDisableMenu(Set<MenuVo> menus, String parentId, Set<MenuVo> p) {
        int code = EGeneralStatus.NORMAL.getCode();
        for (MenuVo menu : menus) {
            if (code == menu.getStatus() && menu.getParentId().equals(parentId)) {
                p.add(menu);
                removeDisableMenu(menus, menu.getId(), p);
            }
        }
        return p;
    }

}